/*
 * Auther:Guan Junming <junming.guan at kisters.cn> Created:12-Oct-2011 14:06:44 Company:Kisters AG
 */
package cn.eclass.servlet;

import cn.eclass.utils.Helper;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author Guan Junming <junming.guan at kisters.cn>
 */
@WebServlet(urlPatterns = {"/do", "/admin/do"})
@MultipartConfig(location = "D:/publish/temp/", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
public class Dispatcher extends HttpServlet {

    private static final String DEFAULT_MODULE = "index";
    private static final String DEFAULT_ACTION = "index";
    private static final String PARAM_MODULE = "module";
    private static final String PARAM_ACTION = "action";
    private static final String ADMIN_MODULE = "admin";
    private static final String FRONT_MODULE = "front";
    HttpServletRequest request;
    HttpServletResponse response;
    public static final Logger logger = Helper.getLogger();

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.request = request;
        this.response = response;
        response.setContentType("text/html;charset=UTF-8");

        String packageName = this.getClass().getPackage().getName();
        String moduleName = this.getDefault(PARAM_MODULE, DEFAULT_MODULE);
        String action = this.getDefault(PARAM_ACTION, DEFAULT_ACTION);
        String subModule = FRONT_MODULE;
        String requestUrl = request.getRequestURL().toString();
        if (requestUrl.contains("admin/do") || requestUrl.endsWith("admin/")) {
            subModule = ADMIN_MODULE;
        }
        String module = packageName + "." + subModule + "." + moduleName;
        try {
            Class.forName(module);
        } catch (ClassNotFoundException ex) {
            module = packageName + "." + moduleName;//common serverlet
        }


        this.doAction(module, action);
    }

    private String getDefault(String parameterName, String defaultValue) {
        String module = request.getParameter(parameterName);
        if (null == module) {
            module = defaultValue;
        }
        return Helper.ucfirst(module);
    }

    private void doAction(String module, String action) {
        ActionFace instance = null;
        try {
            instance = (ActionFace) Class.forName(module).newInstance();
            instance.setRequestAndResponse(request, response);
            instance.execute(instance, action);
        } catch (InstantiationException ex) {
            logger.error(ex);
        } catch (IllegalAccessException ex) {
            logger.error(ex);
        } catch (ClassNotFoundException ex) {
            logger.error(ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
